const Trie = require("../template")

function testTrieAdvancedOperations() {
  const trie = new Trie()

  console.log(
    "1. 删除操作测试:",
    (() => {
      trie.insert("hello")
      trie.insert("world")
      trie.insert("hi")
      trie.delete("hello")
      return trie.search("hello") === false &&
             trie.search("world") === true &&
             trie.search("hi") === true &&
             trie.startsWith("hel") === false
    })()
  )

  console.log(
    "2. 获取所有单词测试:",
    (() => {
      const getAllTrie = new Trie()
      getAllTrie.insert("cat")
      getAllTrie.insert("car")
      getAllTrie.insert("card")
      getAllTrie.insert("care")
      const words = getAllTrie.getAllWords()
      const expected = ["cat", "car", "card", "care"]
      return JSON.stringify(words.sort()) === JSON.stringify(expected.sort())
    })()
  )

  console.log(
    "3. 获取指定前缀的单词测试:",
    (() => {
      const prefixTrie = new Trie()
      prefixTrie.insert("cat")
      prefixTrie.insert("car")
      prefixTrie.insert("card")
      prefixTrie.insert("care")
      prefixTrie.insert("dog")
      const words = prefixTrie.getWordsWithPrefix("car")
      const expected = ["car", "card", "care"]
      return JSON.stringify(words.sort()) === JSON.stringify(expected.sort())
    })()
  )

  console.log(
    "4. 删除后重新插入测试:",
    (() => {
      const deleteInsertTrie = new Trie()
      deleteInsertTrie.insert("test")
      deleteInsertTrie.delete("test")
      deleteInsertTrie.insert("test")
      return deleteInsertTrie.search("test") === true &&
             deleteInsertTrie.startsWith("te") === true
    })()
  )

  console.log(
    "5. 部分删除测试:",
    (() => {
      const partialTrie = new Trie()
      partialTrie.insert("category")
      partialTrie.insert("cat")
      partialTrie.delete("cat")
      return partialTrie.search("category") === true &&
             partialTrie.search("cat") === false &&
             partialTrie.startsWith("cat") === true
    })()
  )

  console.log(
    "6. 空前缀获取单词测试:",
    (() => {
      const emptyPrefixTrie = new Trie()
      emptyPrefixTrie.insert("hello")
      emptyPrefixTrie.insert("world")
      const words = emptyPrefixTrie.getWordsWithPrefix("")
      const expected = ["hello", "world"]
      return JSON.stringify(words.sort()) === JSON.stringify(expected.sort())
    })()
  )

  console.log(
    "7. 不存在的单词删除测试:",
    (() => {
      const nonExistentTrie = new Trie()
      nonExistentTrie.insert("hello")
      nonExistentTrie.delete("world")
      return nonExistentTrie.search("hello") === true &&
             nonExistentTrie.search("world") === false
    })()
  )

  console.log(
    "8. this上下文高级操作测试:",
    (() => {
      const advancedObj = {
        multiplier: 2,
        processAdvanced: function(words) {
          const trie = new Trie()
          words.forEach(word => trie.insert(word))
          const allWords = trie.getAllWords()
          return allWords.length * this.multiplier
        }
      }
      return advancedObj.processAdvanced(["a", "b", "c"]) === 6 // 3 * 2 = 6
    })()
  )

  console.log(
    "9. 复杂对象this绑定高级操作测试:",
    (() => {
      const advancedConfigObj = {
        config: { prefix: "word_" },
        processWithConfig: function(words) {
          const trie = new Trie()
          words.forEach(word => trie.insert(this.config.prefix + word))
          const prefixWords = trie.getWordsWithPrefix("word_")
          return prefixWords.length
        }
      }
      return advancedConfigObj.processWithConfig(["a", "b", "c"]) === 3
    })()
  )

  console.log(
    "10. 高级操作一致性测试:",
    (() => {
      const consistencyTrie = new Trie()
      consistencyTrie.insert("hello")
      consistencyTrie.insert("world")
      consistencyTrie.insert("hi")
      const allWords = consistencyTrie.getAllWords()
      const prefixWords = consistencyTrie.getWordsWithPrefix("h")
      trie.delete("hi")
      const afterDelete = consistencyTrie.getAllWords()
      
      return allWords.length === 3 &&
             prefixWords.length === 2 &&
             afterDelete.length === 2
    })()
  )

  console.log(
    "11. 高级操作边界测试:",
    (() => {
      const boundaryTrie = new Trie()
      boundaryTrie.insert("a")
      boundaryTrie.insert("ab")
      boundaryTrie.insert("abc")
      boundaryTrie.delete("a")
      const words = boundaryTrie.getAllWords()
      const prefixWords = boundaryTrie.getWordsWithPrefix("ab")
      
      return words.length === 2 &&
             prefixWords.length === 2 &&
             boundaryTrie.search("a") === false &&
             boundaryTrie.search("ab") === true
    })()
  )

  console.log(
    "12. 高级操作性能测试:",
    (() => {
      const performanceTrie = new Trie()
      const words = Array.from({ length: 100 }, (_, i) => `word${i}`)
      const start = performance.now()
      words.forEach(word => performanceTrie.insert(word))
      const allWords = performanceTrie.getAllWords()
      words.forEach(word => performanceTrie.delete(word))
      const end = performance.now()
      
      return allWords.length === 100 && (end - start) < 1000
    })()
  )
}

testTrieAdvancedOperations()